package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Supplier;
import com.example.elderlycare.dao.mapper.SupplierMapper;
import com.example.elderlycare.service.SupplierService;
import com.example.elderlycare.service.entity.SupplierItem;
import com.example.elderlycare.service.entity.SupplierListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    @Resource
    SupplierMapper supplierMapper;

    /**
     * querySupplierList 查询供应商列表
     *
     * @param params 查询参数
     * @return SupplierListResponseBody
     */
    @Override
    public SupplierListResponseBody querySupplierList(Map<String, Object> params) {
        SupplierListResponseBody.SupplierListResponseBodyBuilder supplierListResponseBodyBuilder = SupplierListResponseBody.builder();
        Integer count = supplierMapper.querySupplierCount(params);
        supplierListResponseBodyBuilder.total(count);

        List<Supplier> supplierList = supplierMapper.querySupplierList(params);
        List<SupplierItem> items = new ArrayList<>();

        supplierList.forEach((item) -> {
            SupplierItem.SupplierItemBuilder supplierItemBuilder = SupplierItem.builder().
                    id(item.getId()).
                    name(item.getName()).
                    code(item.getCode()).
                    phone(item.getPhone()).
                    email(item.getEmail()).
                    address(item.getAddress()).
                    principal(item.getPrincipal()).
                    equipType(item.getEquipType() == null ? "未定义" : item.getEquipType().getName());

            items.add(supplierItemBuilder.build());
        });

        supplierListResponseBodyBuilder.items(items);

        return supplierListResponseBodyBuilder.build();
    }
}
